export function handleAnswer() {
  const answerList = JSON.parse(localStorage.getItem('testInfo'));
  const answer = [];
  answerList.map(item => {
    if (item.type === 2 || item.type === 8 || item.type === 18 || item.type === 19) {
      // 多选
      answer.push({
        answer: item.value.join(','),
        question_id: item.id,
        is_right: compare(item.answer.split(''), item.value).length === item.answer.split('').length && item.answer.split('').length === item.value.length ? 1 : 0,
        remarks: !!item.remarks
      })
    } else {
      // 单选
      answer.push({
        answer: item.value,
        question_id: item.id,
        is_right: item.value === item.answer ? 1 : 0,
        remarks: !!item.remarks
      })
    }
  })
  return answer;
}
export function handleScore(data) {
  const answerArr = JSON.parse(localStorage.getItem('testInfo')).filter(item => item.value.length !== 0);
  let total = 0;
  answerArr.map(item => {
    if (item.type === 2 || item.type === 8 || item.type === 18 || item.type === 19) {
      // if (item.app_id == 82 || item.app_id == 60) {
      if ((item.app_id == 82 || item.app_id == 60 || item.app_id == 61 || item.app_id == 62) && item.type === 8) {
        const answerOp = item.answer.split('');
        const errorNum = compare(answerOp, item.value, true).length;// 计算错误个数
        if (answerOp.length - errorNum > 0) {
          const score = ((answerOp.length - errorNum) / answerOp.length) * item.score
          total += score
        } else {
          total += 0
        }
      } else {
        const answerOp = item.answer.split('');
        const rightNum = compare(answerOp, item.value);// 计算正确个数
        if (rightNum.length === answerOp.length && answerOp.length === item.value.length) {
          // 正确
          total += item.score
        } else {
          total += 0
        }
      }
    } else {
      if (item.value == item.answer) {
        total += item.score
      }
    }
  })
  return total
}
function compare(arr1, arr2, type) {
  if (type) {
    // 计算错误个数
    if (arr1.length >= arr2.length) {
      const result = arr1.filter(element => !arr2.includes(element));
      return result
    } else {
      const commonElements = arr1.filter(element => arr2.includes(element));
      const difference = arr2.filter(element => !commonElements.includes(element));
      return difference
    }
  } else {
    // 计算正确个数
    return arr1.filter((v) => {
      return arr2.includes(v);
    });
  }
}
// 全屏
export function fullScreen() {
  const element = document.documentElement;
  if (element.requestFullscreen) {
    element.requestFullscreen();
  } else if (element.mozRequestFullScreen) {
    element.mozRequestFullScreen();
  } else if (element.webkitRequestFullscreen) {
    element.webkitRequestFullscreen();
  } else if (element.msRequestFullscreen) {
    element.msRequestFullscreen();
  }
}
// 取消全屏
export function turnFullScreen() {
  if (document.exitFullscreen) {
    document.exitFullscreen();
  } else if (document.webkitCancelFullScreen) {
    document.webkitCancelFullScreen();
  } else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
  } else if (document.msExitFullscreen) {
    document.msExitFullscreen();
  }
}
// 处理试卷题目
export function testData(data) {

}
